import re

import requests
from model.park_spider import ParkSpider
from model.park_platform import ParkPlatform
from model.car_number import CarNumber
from model.result import Result
from model.error import ParkSpiderError, ParkSpiderErrorCode, TokenExpireError

import dateutil.parser as dateutil

from urllib import parse
import common.http as http
import spider_config
import json
import datetime
import time
from common.logger import logger

# Host: mops.fujica.com.cn
# Cookie: WapPay_UserInfo=DFXfRm4ivPBNIoC7a3yxa8B1Tf/MiQpE4BU6BkDp7PDkjxFF1cdLWbxC9WK2xPBNFHJAplG9xJsYMDg8w31UCNf/IzIbmrbtrPGfoLwt0AXszitFYxwvHikJMYp84Xqqtc45NSlbW4xti/MRyNU8onTzzQ8qb1cpNBLOhX4toS8eqpcF4hgfm9fWKvLK4EIgrFXlciCdBO2XM+t8IrPnV3FxuuhR5NS9DhEA1em4Mr+2WgG+pgFsE6vXm8yI0Awt;
# Connection: keep-alive
# Upgrade-Insecure-Requests: 1
# Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
# User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.33(0x18002120) NetType/WIFI Language/en
# Referer: http://mops.fujica.com.cn/v3/OtherAgent?key=FUJICA97
# Accept-Language: zh-CN,zh-Hans;q=0.9
# Accept-Encoding: gzip, deflate
# Connection: Keep-Alive


class AibokeParkSpider(ParkSpider):
    def platform(self) -> ParkPlatform:
        return ParkPlatform(spider_config.aiboke_park_id)

    def query(self, carNumber: CarNumber) -> Result:
        result = Result()
        result.platform = self.platform()
        result.car_number = carNumber

        url = "http://mops.fujica.com.cn/v3/Bussiness/GetParkInfo"

        headers = {
            "Host": "mops.fujica.com.cn",
            "Cookie": "WapPay_UserInfo=DFXfRm4ivPBNIoC7a3yxa8B1Tf/MiQpE4BU6BkDp7PDkjxFF1cdLWbxC9WK2xPBNFHJAplG9xJsYMDg8w31UCNf/IzIbmrbtrPGfoLwt0AXszitFYxwvHikJMYp84Xqqtc45NSlbW4xti/MRyNU8onTzzQ8qb1cpNBLOhX4toS8eqpcF4hgfm9fWKvLK4EIgrFXlciCdBO2XM+t8IrPnV3FxuuhR5NS9DhEA1em4Mr+2WgG+pgFsE6vXm8yI0Awt;",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.33(0x18002120) NetType/WIFI Language/en",
        }

        params = {
            "carNo": str(carNumber),
        }

        rsp = http.get(
            url,
            headers=headers,
            params=params,
            proxies=self.getProxy(),
            timeout=self.getTimeout(),
        )

        if rsp.status_code != 200:
            logger.debug(f"爱泊客反爬: {rsp.status_code}")
            raise requests.exceptions.ProxyError(f"爱泊客反爬: {rsp.status_code}")

        # 正则匹配 寻找 ParkingName&quot;:&quot;银海幸福广场&quot;
        # 正则匹配 寻找 BeginTime&quot;:&quot;2023-03-08 18:07:49&quot;
        # 正则匹配 寻找 &quot;ParkingFee&quot;:9.00,&quot

        r_park = re.search(r"ParkingName&quot;:&quot;(.*?)&quot;", rsp.text)
        r_time = re.search(r"BeginTime&quot;:&quot;(.*?)&quot;", rsp.text)
        r_fee = re.search(r"&quot;ParkingFee&quot;:(.*?),&quot", rsp.text)

        if r_park is None or r_time is None or r_fee is None:
            return result

        result.success = True
        result.park_name = r_park.group(1)
        result.price = r_fee.group(1)
        result.enter_time = dateutil.parse(r_time.group(1))
        result.platform = self.platform()

        logger.debug(f"爱泊客查询结果: {result}")

        return result
